package com.healthy.controller.pc;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.healthy.entity.YgAdmins;
import com.healthy.service.YgCharpterService;

/**
 * @author Maple.Chen
 * @date 2020年6月12日
 * @description <p>文章相关控制器</p>
 */
@RestController
public class YgPcCharpterController {
	
	@Autowired
	private YgCharpterService ycs;
	
	// PC管理员发布文章
	@RequestMapping(value = "/chap/postchap", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public JSONObject postCharpter(
			@RequestParam("upfile") MultipartFile file,
			HttpServletRequest request, HttpServletResponse response){
		YgAdmins yadmin = (YgAdmins) request.getSession().getAttribute("admin");
		String admin_id = yadmin.getAid();
		JSONObject json = ycs.saveCharpter(request, file, admin_id);
		return json;
	}
	
	// 根据文章id查询某一篇文章
	@RequestMapping(value = "/chap/querychap", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public JSONObject queryCharpter(HttpServletRequest request){
		String did = StringUtils.isBlank( request.getParameter("did") ) ? null : request.getParameter("did");
		JSONObject json = ycs.queryCharpterById(did);
		return json;
	}
	
	// 首页展示文章
	@RequestMapping(value = "/chap/mainchap", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public JSONObject mainExhibition(
			@RequestParam("pageNum") int pageNum,  // 当前页数
			@RequestParam("pageSize") int pageSize    // 每页展示记录条数
			){
		JSONObject json = ycs.queryAllOfCharpters(pageNum, pageSize);
		return json;
	}
	
	// 文章匹配：分类或模糊信息搜索
	@RequestMapping(value = "/chap/searchchap", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public JSONObject fuzzyMatch(
			@RequestParam("pageNum") int pageNum,  // 当前页数
			@RequestParam("pageSize") int pageSize,    // 每页展示记录条数
			HttpServletRequest request
			){
		JSONObject json = ycs.queryCharptersByCondition(pageNum, pageSize, request);
		return json;
	}
	
	// 管理员查看自己发布的文章
	@RequestMapping(value = "/chap/ad-self-c", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public JSONObject adminSelfChapter(
			@RequestParam("pageNum") int pageNum,  // 当前页数
			@RequestParam("pageSize") int pageSize,    // 每页展示记录条数
			HttpServletRequest request
			){
		YgAdmins yadmin = (YgAdmins) request.getSession().getAttribute("admin");
		String admin_id = yadmin.getAid();
		JSONObject json = ycs.queryCharptersByAdminId(pageNum, pageSize, admin_id);
		return json;
	}
	
	// 管理员修改自己发表的文章
	@RequestMapping(value = "/chap/ad-update-c", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public JSONObject adminUpdateChapterForSelf(
			@RequestParam("upfile") MultipartFile file,
			HttpServletRequest request
			){
		YgAdmins yadmin = (YgAdmins) request.getSession().getAttribute("admin");
		String admin_id = yadmin.getAid();
		JSONObject json = ycs.updateCharpter(file, request, admin_id);
		return json;
	}
	
	// 管理员删除自己发表的文章
	@RequestMapping(value = "/chap/ad-del-c", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
	public JSONObject adminDelChapterForSelf(
			@RequestBody Map<String, Object> map,
			HttpServletRequest request
			){
		YgAdmins yadmin = (YgAdmins) request.getSession().getAttribute("admin");
		String admin_id = yadmin.getAid();
		JSONObject json = null;
		String did = "";
		try{
			did = (String) map.get("did");
		}catch(Exception e){
			e.printStackTrace();
			json = new JSONObject();
			json.put("status", -1);
			json.put("msg", "请求参数格式错误");
			return json;
		}
		json = ycs.deleteCharpter(request, did, admin_id);
		return json;
	}
	
	// 查看所有文章分类
	@RequestMapping(value = "/chap/queryalltype", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public JSONObject queryAllCharpterType(){
		JSONObject json = ycs.queryAllCharpterTypes();
		return json;
	}
}
